package com.education.platform.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.education.platform.common.Result;
import com.education.platform.dto.DashboardOverviewDTO;
import com.education.platform.dto.OrderStatsDTO;
import com.education.platform.dto.UserTrendDTO;
import com.education.platform.entity.Statistics;
import com.education.platform.service.StatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 统计控制器
 *
 * @author education-platform
 * @since 2024-09-27
 */
@Tag(name = "数据统计", description = "数据统计相关接口")
@RestController
@RequestMapping("/api/statistics")
@RequiredArgsConstructor
public class StatisticsController {

    private final StatisticsService statisticsService;

    @Operation(summary = "创建统计", description = "创建统计记录（管理员）")
    @PostMapping
    public Result<Long> createStatistics(@Validated @RequestBody Statistics statistics) {
        Long statisticsId = statisticsService.createStatistics(statistics);
        return Result.success(statisticsId);
    }

    @Operation(summary = "更新统计", description = "更新统计记录（管理员）")
    @PutMapping("/{id}")
    public Result<Void> updateStatistics(@PathVariable Long id, @Validated @RequestBody Statistics statistics) {
        statisticsService.updateStatistics(id, statistics);
        return Result.success();
    }

    @Operation(summary = "删除统计", description = "删除统计记录（管理员）")
    @DeleteMapping("/{id}")
    public Result<Void> deleteStatistics(@PathVariable Long id) {
        statisticsService.deleteStatistics(id);
        return Result.success();
    }

    @Operation(summary = "获取统计详情", description = "根据ID获取统计详细信息（管理员）")
    @GetMapping("/{id}")
    public Result<Statistics> getStatisticsDetail(@PathVariable Long id) {
        Statistics statistics = statisticsService.getStatisticsById(id);
        return Result.success(statistics);
    }

    @Operation(summary = "根据日期和类型获取统计", description = "根据日期和类型获取统计（管理员）")
    @GetMapping("/date-type")
    public Result<Statistics> getStatisticsByDateAndType(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate statDate,
            @RequestParam String statType) {
        Statistics statistics = statisticsService.getStatisticsByDateAndType(statDate, statType);
        return Result.success(statistics);
    }

    @Operation(summary = "查询日期范围统计", description = "查询指定日期范围内的统计（管理员）")
    @GetMapping("/range")
    public Result<List<Statistics>> getStatisticsByDateRange(
            @RequestParam String statType,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        List<Statistics> statistics = statisticsService.getStatisticsByDateRange(statType, startDate, endDate);
        return Result.success(statistics);
    }

    @Operation(summary = "获取最新统计", description = "获取指定类型的最新统计（管理员）")
    @GetMapping("/latest")
    public Result<Statistics> getLatestStatistics(@RequestParam String statType) {
        Statistics statistics = statisticsService.getLatestStatistics(statType);
        return Result.success(statistics);
    }

    @Operation(summary = "统计总和", description = "统计指定类型和日期范围的总和（管理员）")
    @GetMapping("/sum")
    public Result<Long> sumByTypeAndDateRange(
            @RequestParam String statType,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        Long sum = statisticsService.sumByTypeAndDateRange(statType, startDate, endDate);
        return Result.success(sum);
    }

    @Operation(summary = "执行每日统计", description = "手动执行每日统计任务（管理员）")
    @PostMapping("/execute-daily")
    public Result<Void> executeDailyStatistics() {
        statisticsService.executeDailyStatistics();
        return Result.success();
    }

    @Operation(summary = "获取Dashboard概览数据", description = "获取Dashboard概览统计数据（管理员）")
    @GetMapping("/overview")
    public Result<DashboardOverviewDTO> getDashboardOverview() {
        DashboardOverviewDTO overview = statisticsService.getDashboardOverview();
        return Result.success(overview);
    }

    @Operation(summary = "获取用户增长趋势", description = "获取用户增长趋势数据（管理员）")
    @GetMapping("/user-trend")
    public Result<UserTrendDTO> getUserTrend(@RequestParam(required = false, defaultValue = "7") Integer days) {
        UserTrendDTO trend = statisticsService.getUserTrend(days);
        return Result.success(trend);
    }

    @Operation(summary = "获取订单状态统计", description = "获取订单状态统计数据（管理员）")
    @GetMapping("/order-stats")
    public Result<OrderStatsDTO> getOrderStats() {
        OrderStatsDTO stats = statisticsService.getOrderStats();
        return Result.success(stats);
    }
}

